package com.lianzhihui.romeex.ui.wallet;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.google.android.material.tabs.TabLayout;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.MyFragmentAdapter;
import com.lianzhihui.romeex.bean.TradeRecordBean;
import com.lianzhihui.romeex.bean.WalletMainBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.config.MessageEvent;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.MathUtil;
import com.lianzhihui.romeex.utils.NumberUtils;
import com.lianzhihui.romeex.utils.UserUtils;
import com.lianzhihui.romeex.widget.MyViewPager;
import com.lianzhihui.romeex.widget.dialog.RoAlertDialog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

import static com.lianzhihui.romeex.utils.UserUtils.getCurrentUserMoney;

/**
 * Description: 可跨链交易币种
 * Author: RAMON
 * CreateDate: 2020/11/6 5:04 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class CoinActivityType3 extends BaseActivity implements TabLayout.OnTabSelectedListener {

    @BindView(R.id.tv_assets)
    TextView tv_assets;
    @BindView(R.id.tv_abort)
    TextView tv_abort;
    @BindView(R.id.tv_type)
    TextView tv_type;

    @BindView(R.id.tv_canuse)
    TextView tv_canuse;
    @BindView(R.id.tv_freeze)
    TextView tv_freeze;

    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    MyViewPager viewPager;
    @BindView(R.id.bt_zhuanzhang)
    Button bt_zhuanzhang;
    @BindView(R.id.bt_shouqian)
    Button bt_shouqian;
    @BindView(R.id.bt_cross_chain)
    Button bt_cross_chain;

    WalletMainBean.CoinBean coin;
    TradeRecordBean tradeBean;
    private ArrayList<Fragment> fragmentList = new ArrayList<>(3);
    MyFragmentAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_coin3_layout;
    }

    @Override
    protected void initView() {
        coin = (WalletMainBean.CoinBean) getIntent().getSerializableExtra("coin");
        setTitle(coin.getCoinNameAlias());
        tv_type.setText(getCurrentUserMoney());
        initViewpager();

        //警告弹窗
        RoAlertDialog dialog = new RoAlertDialog(this);
        dialog.show();
    }

    @Override
    protected void initData() {

    }

    public void initViewpager() {

        fragmentList.add(TradeRecordFragment.newInstance(coin, 0));
        fragmentList.add(TradeRecordFragment.newInstance(coin, 1));
        fragmentList.add(TradeRecordFragment.newInstance(coin, 2));
        FragmentManager fragmentManager = getSupportFragmentManager();
        adapter = new MyFragmentAdapter(fragmentManager, fragmentList);
        viewPager.setAdapter(adapter);
        viewPager.setScrollable(false);
        viewPager.setOffscreenPageLimit(2);
        tabLayout.addOnTabSelectedListener(this);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onMessageEvent(MessageEvent event) {
        super.onMessageEvent(event);
        if (event.getCode() == AppConstants.Event.REFRESHER_COIN) {
            tradeBean = (TradeRecordBean) event.getValue();

            tv_assets.setText(NumberUtils.big2(tradeBean.getTotalBalance()));
            double res = 0d;
            switch (UserUtils.getCurrentyType()) {
                case 1:
                    res = MathUtil.multiply(tradeBean.getTotalBalance(), tradeBean.getPriceCny());
                    tv_abort.setText(NumberUtils.big2(res));
                    break;
                case 2:
                    res = MathUtil.multiply(tradeBean.getTotalBalance(), tradeBean.getPrice());
                    tv_abort.setText(NumberUtils.big2(res));
                    break;
                default:
                    break;
            }

            tv_canuse.setText(NumberUtils.big8(tradeBean.getValidBalance()));
            tv_freeze.setText(NumberUtils.big8(tradeBean.getLockBalance()));
        }
    }

    @OnClick({R.id.bt_zhuanzhang, R.id.bt_shouqian, R.id.bt_cross_chain})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_zhuanzhang:
                Intent intent = new Intent(this, ZhuanZhangActivity.class);
                intent.putExtra("coin", coin);
                startActivity(intent);
                break;
            case R.id.bt_shouqian:
                Intent intent2 = new Intent(this, ShouqianActivity.class);
                intent2.putExtra("coin", coin);
                startActivity(intent2);
                break;
            case R.id.bt_cross_chain:
                Intent intent3 = new Intent(this, CrossChainActivity.class);
                intent3.putExtra("coin", coin);
                startActivity(intent3);
                break;
            default:
                break;
        }
    }
}
